package heven.holt.eyepetizer.adapter

import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import heven.holt.eyepetizer.BR
import heven.holt.eyepetizer.adapter.holder.CommendViewHolder
import heven.holt.eyepetizer.model.BasePageModel

class CommendAdapter :
    PagingDataAdapter<BasePageModel.Item, RecyclerView.ViewHolder>(DiffCallback()) {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder =
        CommendViewHolder.getViewHolder(parent, viewType)

    override fun getItemViewType(position: Int): Int =
        CommendViewHolder.getItemViewType(getItem(position)!!)

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        if (holder !is CommendViewHolder.AbstractCommendViewHolder) return
        holder.binding.setVariable(BR.item, getItem(position)!!)
        holder.binding.executePendingBindings()
    }

    class DiffCallback : DiffUtil.ItemCallback<BasePageModel.Item>() {
        override fun areItemsTheSame(
            oldItem: BasePageModel.Item,
            newItem: BasePageModel.Item
        ): Boolean {
            return oldItem.data?.header?.id == newItem.data?.header?.id
        }

        override fun areContentsTheSame(
            oldItem: BasePageModel.Item,
            newItem: BasePageModel.Item
        ): Boolean {
            return oldItem == newItem
        }
    }
}